package com.example.service.impl;

import com.example.mapper.MaterialMapper;
import com.example.mapper.OrderMapper;
import com.example.mapper.OutMaterialsMapper;
import com.example.mapper.OutputAllMapper;
import com.example.pojo.*;
import com.example.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private MaterialMapper materialMapper;
    @Autowired
    private OutputAllMapper outputAllMapper;
    @Autowired
    private OutMaterialsMapper outMaterialsMapper;

    @Override
    public PageBean page(Integer page, Integer pageSize, int materialID, int quantity, String status, LocalDate begin, LocalDate end) {
        Long count = orderMapper.count(materialID,quantity,status,begin,end);//总数返回
        Integer start=(page-1)*pageSize;//计算起始位置
        List<Order> empList = orderMapper.page(start, pageSize,materialID,quantity,status,begin,end);//该页记录数返回

        PageBean pageBean = new PageBean(count, empList);
        return pageBean;
    }

    @Override
    public void add(Order order) {
        order.setOrderDate(LocalDate.now());
        orderMapper.add(order);
    }

    @Override
    public void deletes(List<Integer> ids) {
        orderMapper.deletes(ids);
    }

    @Override
    public void finishOrder(Order order) {
        orderMapper.finishOrder(order);
        materialMapper.updates(order);
        int sum=order.getQuantity()*materialMapper.getprice(order.getMaterialID());
        String name = materialMapper.getName(order.getMaterialID());
        String unit = materialMapper.getunit(name);
        OutMaterials outMaterials=new OutMaterials();
        outMaterials.setMname(name);
        outMaterials.setNum(order.getQuantity());
        outMaterials.setPrice(materialMapper.getprice(order.getMaterialID()));
        outMaterials.setTime(LocalDate.now());
        outMaterials.setTotal(sum);
        outMaterials.setNote("购买入库"+name+order.getQuantity()+unit+"成功,花费"+sum+"元");
        outMaterialsMapper.add(outMaterials);

        OutputMater outputMater=new OutputMater();
        outputMater.setOrderID(order.getOrderID());
        outputMater.setMaterialID(order.getMaterialID());
        outputMater.setQuantity(order.getQuantity());
        outputMater.setTotal(sum);
        outputMater.setDate(LocalDate.now());
        outputAllMapper.add("购买序号"+order.getMaterialID()+"件"+order.getQuantity()+"个"+name+"成功",sum,LocalDate.now());
        orderMapper.add_order(outputMater);
    }
}
